'''
Created on 27.5.2011

@author: Martin Vegi Kysel
'''
import re

def extractNonDigitPrefix(digitPositionString):
    '''
    @summary: returns the prefix of the input separated by the first digit, ignores file type
    @param string: input
    @return: substring 
    '''
    
    # take only the part before "." = ignore file type
    digitPositionString = digitPositionString.partition(".")[0]
    
    strLen = len(digitPositionString)
    firstPosition = 0
    
    # use a inverted range strLen:0
    for i in range (strLen):
        
        #get the symbol at the position starting from the end
        value =  digitPositionString[strLen-i-1]
        
        # first nonDigit value breaks the loop
        if not str.isdigit(value):
            
            # save the position of the last digit
            firstPosition = strLen-i
            break
        
    # return the substring 0 to the first digit(exclusive)
    return digitPositionString[0:firstPosition]

def trimAllWhitespace(s):
    '''
    @param s: string with whitespace
    @return: input strings with whitespace removed
    @summary: Trims all whitespace from the input string
    '''
    pat = re.compile(r'\s+')
    return pat.sub('', s)

def findAll(s,symbol):
    '''
    @param s: input string
    @param symbol: character/string to be found 
    @return: list of positions of the symbol
    @summary: Finds the position of all occurrences of char symbol in string s
    '''
    return [match.start() for match in re.finditer(re.escape(symbol), s)]

def printRoundedArray(x):
    '''
    @param x: array of values 
    @return: none
    @summary: prints rounded values to three decimal spaces
    '''
    for current in x:
        print "%.3f" % current

def roundArray(x, places):
    '''
    @param x: array of values
    @param places: number of decimal places
    @return: rounded array to _places_ decimal places
    @bug: depends on python version
    '''
    returnArray = []
    for current in x:
        value = round(current,3)
        returnArray.append(value)
    return returnArray
        
def isAFloat(s):
    '''
    @param s: string to be tested
    @return: boolean decision
    @summary: tests if a string is a float, 
                does not work simple isDigit() because decimal numbers are categorized wrongly 
    '''
    try:
        float(s)
        return True
    except ValueError:
        return False
    
def isAInteger(s):
    '''
    @param s: string to be tested
    @return: boolean decision
    @summary: tests if a string is a integer, 
                does not work simple isDigit() because decimal numbers are categorized wrongly 
    '''
    try:
        int(s)
        return True
    except ValueError:
        return False
